#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 100010, INF = 0x3f3f3f3f;
int h[N], e[N], ne[N], w[N], idx;
int dist[N];
bool st[N];
int n, m;
void add(int a, int b, int c)
{
    e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
void spfa()
{
    memset(dist, 0x3f, sizeof dist);
    queue<int> q;
    q.push(1);
    st[1] = true;
    dist[1] = 0;
    while (q.size())
    {
        int ver = q.front();
        q.pop();
        st[ver] = false;
        for (int i = h[ver]; ~i; i = ne[i])
        {
            int j = e[i];
            if (dist[ver] + w[i] < dist[j])
            {
                dist[j] = dist[ver] + w[i];
                if (st[j] == false)
                {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    memset(h, -1, sizeof h);
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }
    spfa();
    if (dist[n] >= INF / 2) cout << "impossible" << endl;
    else cout << dist[n] << endl;
    return 0;
}